﻿//This C# code file was released under the Ms-PL license
//http://www.opensource.org/licenses/ms-pl.html
//This script was originally intended for use with Microsoft Dynamics AX
//and maintained and distributed as a project on CodePlex
//http://dynamicsaxadmin.codeplex.com

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Win32;

namespace CodeCrib.AX2009.Manage
{
    public class AppFiles
    {
        string appFilesFolder;

        //public static void ExtractModelInfo(string modelManifest, out string publisher, out string modelName, out string layer)

        public AppFiles(string appFilesFolder)
        {
            this.appFilesFolder = appFilesFolder;
        }

        //public bool ModelExist(string modelName, string modelPublisher, string layer = "")

        public void ExportLayer(string layer, string destinationFolder)
        {
            System.IO.File.Copy(string.Format("{0}\\ax{1}.aod", appFilesFolder, layer), string.Format("{0}\\ax{1}.aod", destinationFolder, layer));
        }

        public void ExportLabels(string labelID, string language, string destinationFolder)
        {
            string labelDef = string.Format("ax{0}{1}.ald", labelID, language);
            string labelComment =string.Format("ax{0}{1}.alc", labelID, language);

            System.IO.File.Copy(
                System.IO.Path.Combine(appFilesFolder, labelDef),
                System.IO.Path.Combine(destinationFolder, labelDef));

            if (System.IO.File.Exists(System.IO.Path.Combine(appFilesFolder, labelComment)))
            System.IO.File.Copy(
                System.IO.Path.Combine(appFilesFolder, labelComment),
                System.IO.Path.Combine(destinationFolder, labelComment));
        }

        public void InstallLabels(string labelID, string language, string sourceFolder)
        {
            string labelDef = string.Format("ax{0}{1}.ald", labelID, language);
            string labelComment = string.Format("ax{0}{1}.alc", labelID, language);

            System.IO.File.Copy(
                System.IO.Path.Combine(sourceFolder, labelDef),
                System.IO.Path.Combine(appFilesFolder, labelDef));

            RemoveReadOnlyFlags(System.IO.Path.Combine(appFilesFolder, labelDef));            

            if (System.IO.File.Exists(System.IO.Path.Combine(sourceFolder, labelComment)))
            {
                System.IO.File.Copy(
                    System.IO.Path.Combine(sourceFolder, labelComment),
                    System.IO.Path.Combine(appFilesFolder, labelComment));

                RemoveReadOnlyFlags(System.IO.Path.Combine(sourceFolder, labelComment));
            }
        }

        //public List<string> GetModelElements(string modelName, string modelPublisher)

        public void InstallLayer(string layerFile)
        {
            var newFile = System.IO.Path.Combine(appFilesFolder, System.IO.Path.GetFileName(layerFile));
            System.IO.File.Copy(layerFile, newFile);
            RemoveReadOnlyFlags(newFile);
        }

        public void UninstallLayer(string layer)
        {
            System.IO.File.Delete(string.Format("{0}\\ax{1}.aod", appFilesFolder, layer));
        }

        public static void RemoveReadOnlyFlags(string filename)
        {
            var attributes = System.IO.File.GetAttributes(filename);
            if ((attributes & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly)
            {
                System.IO.File.SetAttributes(filename, attributes & ~System.IO.FileAttributes.ReadOnly);
            }
        }

        public static void RemoveReadOnlyFlags(string directoryName, string filePattern)
        {
            if (System.IO.Directory.Exists(directoryName))
            {
                IEnumerable<string> files = System.IO.Directory.GetFiles(directoryName, filePattern);
                foreach (string file in files)
                {
                    RemoveReadOnlyFlags(file);
                }
            }
        }
    }
}
